﻿#region copyright info
//------------------------------------------------------------------------------
// <copyright company="ChaosStudio">
//     Copyright (c) 2002-2010 巧思工作室.  All rights reserved.
//     Contact:		MSN:zhouyu@cszi.com , QQ:478779122
//		Link:				http://www.69sea.com http://www.cszi.com
// </copyright>
//------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;

namespace CS
{
    /// <summary>
    ///  简单的类型处理工具
    ///  ver:0.3.0
    /// </summary>
    /// 
    /// <description class = "CS.ParamTool">
    ///   参数处理工具Method(入参,默认值,最小值,最大值);
    /// <para>
    /// Note:转换是否失败不可知，所有转换失败或是超过范围的的都将转为默认值。
    /// </para>
    /// <para>更高级的参数处理请使用ParamHelper</para>
    /// </description>
    /// 
    /// <history>
    ///   2010-4-12 16:34:45 , zhouyu ,  创建	
    ///   2010-4-13 , zhouyu , 增加范围转换  
    ///   2010-9-25 , zhouyu , 重新整理，调整defaultValue至第二个参数位   
    ///   2010-12-09 , zhouyu , 使用3.5的新属性，扩展字符串
    ///  </history>
    public static class ParamTool
    {
        
        #region Bool()  bool 类型处理

        /// <summary>
        /// 将参数p转换为bool，默认值为false
        /// </summary>
        /// <param name="p">需要转换的文本</param>
        /// <param name="trueStrings">判定为真时的字符串变参，默认: "1", "true", "on" (不区分大小写)。</param>
        /// <returns>转换结果</returns>
        public static bool Bool(string p,params string[] trueStrings)
        {
            return Bool(p, false, trueStrings);
        }
        

        /// <summary>
        /// 将参数p转换为bool
        /// </summary>
        /// <param name="p">需要转换的文本</param>
        /// <param name="defaultValue">转换失败时的值</param>
        /// <param name="trueChars">判定为真时的字符串变参，默认: "1", "true", "on" (不区分大小写)。</param>
        /// <returns>转换结果</returns>
        public static bool Bool(string p,bool defaultValue,params string[] trueChars)
        {
            bool result;
            var chars = trueChars.Length == 0 ? new[] { "1", "true", "on" } : trueChars;
            TryBool(p, defaultValue, out result, chars);
            return result;
        }

        /// <summary>
        /// 参数P是否被成功转换,类似于TryParse
        /// <para>zhouyu,2010-09-20</para>
        /// </summary>
        /// <param name="p">需要转换的文本</param>
        /// <param name="defaultValue">默认值，转换失败时输出的值</param>
        /// <param name="result">输出转换的结果</param>
        /// <param name="trueChars">判定为真的字符串的变参</param>
        /// <returns>是否转换成功</returns>
        public static bool TryBool(string p, bool defaultValue, out bool result, params string[] trueChars)
        {
            var rst = false; //默认转换失败
            result = defaultValue;  //默认结果为默认值
            foreach (var s in trueChars)
            {
                if (!p.Equals(s,StringComparison.CurrentCultureIgnoreCase)) continue;
                rst = true;
                result = true;
                break;
            }
            if(!rst)
                rst = Boolean.TryParse(p, out result);
            return rst;
        }

        /// <summary>
        /// 判断对象是否为空
        /// </summary>
        /// <param name="p">对像引用</param>
        /// <returns>非空为真，否则为假</returns>
        public static bool Bool(object p)
        {
            return Bool(p, false);
        }

        /// <summary>
        /// 将object 转换为bool ， null或转换失败时为默认值
        /// </summary>
        /// <param name="p">需要转换的object</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换结果</returns>
        public static bool Bool(object p, bool defaultValue)
        {
            if (p == null)return defaultValue;

            if (p.GetType() == typeof(string))return Bool(p.ToString(), defaultValue);

            if (p.GetType() == typeof(bool))return (bool)p;

            return defaultValue;
        }

        #endregion


        #region Byte() btye 类型处理

        /// <summary>
        /// 将字符串转换为 byete
        /// </summary>
        /// <param name="param">需要转换的文本</param>
        /// <returns>转换结果</returns>
        public static byte Byte(string param)
        {
            return Byte(param, 0);
        }

        /// <summary>
        /// byte 类型转换
        /// </summary>
        /// <param name="p">字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <returns>转换结果</returns>
        public static byte Byte(string p,byte defaultValue,byte min,byte max)
        {
            var r = Byte(p, defaultValue);
            return Byte(r, defaultValue, min, max);
        }

        /// <summary>
        /// 将字符串转换为 byte
        /// </summary>
        /// <param name="param">需要转换的文本</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换结果</returns>
        public static byte Byte(string param, byte defaultValue)
        {
            byte result;
            return !System.Byte.TryParse(param, out result) ? defaultValue : result;
        }

        /// <summary>
        /// byte 范围转换
        /// </summary>
        /// <param name="p">输入</param>
        /// <param name="defalutValue">默认值</param>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <returns>不介于最小最大值间时返回默认值</returns>
        public static byte Byte(byte p, byte defalutValue, byte min, byte max)
        {
            if (min <= p && p <= max) return p;
            return defalutValue;
        }

        /// <summary>
        /// 参数范围转换
        /// </summary>
        /// <param name="defalutValue">默认值</param>
        /// <param name="obj">输入</param>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <returns>不介于最小最大值间时返回默认值</returns>
        public static byte Byte(object obj, byte defalutValue, byte min, byte max)
        {
            var r = Byte(obj, defalutValue);
            return Byte(r, defalutValue, min, max);
        }

        /// <summary>
        /// 将object 转换为bype，null或是转换失败时为默认值
        /// </summary>
        /// <param name="param">需要转换的 object</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换结果</returns>
        public static byte Byte(object param, byte defaultValue)
        {
            if (param == null) return defaultValue;

            if (param.GetType() == typeof(object)) return Byte(param.ToString(), defaultValue);

            if (param.GetType() == typeof(byte)) return (byte)param;

            return defaultValue;
        }

        #endregion


        #region Short() short  int16 类型处理

        /// <summary>
        /// 将字符串转换为 short , 默认值0
        /// </summary>
        /// <param name="param">需要转换的 文本</param>
        /// <returns>转换结果</returns>
        public static short Short(string param)
        {
            return Short(param, 0);
        }

        /// <summary>
        /// 范围约束
        /// </summary>
        /// <param name="p">输入的值</param>
        /// <param name="defaultValue">默认值</param>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <returns>不介于最小最大之间时返回默认值</returns>
        public static short Short(string p, short defaultValue, short min, short max)
        {
            var r = Short(p, defaultValue);
            return Short(r, defaultValue, min, max);
        }

        /// <summary>
        /// 范围约束
        /// </summary>
        /// <param name="p">输入的值</param>
        /// <param name="defaultValue">默认值</param>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <returns>不介于最小最大之间时返回默认值</returns>
        public static short Short(short p, short defaultValue, short min, short max)
        {
            if (min <= p && p <= max) return p;
            return defaultValue;
        }

        /// <summary>
        /// 将对像转为Short, 默认值0
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static short Short(object  p)
        {
            return Short(p,0);
        }

        /// <summary>
        /// 将 object 转换为 short
        /// </summary>
        /// <param name="param">需要转换的 object</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换结果</returns>
        public static short Short(object param, short defaultValue)
        {
            if (param == null) return defaultValue;

            if (param.GetType() == typeof(string)) return Short(param.ToString(), defaultValue);

            if (param.GetType() == typeof(short)) return (short)param;

            return defaultValue;
        }  
        
        /// <summary>
        /// 范围约束
        /// </summary>
        /// <param name="obj">输入的值</param>
        /// <param name="defaultValue">默认值</param>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <returns>不介于最小最大之间时返回默认值</returns>
        public static short Short(object obj, short defaultValue, short min, short max)
        {
            var r = Short(obj, defaultValue);
            return Short(r, defaultValue, min, max);
        }

        /// <summary>
        /// 将字符串转换为 short
        /// </summary>
        /// <param name="param">需要转换的 文本</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换结果</returns>
        public static short Short(string param, short defaultValue)
        {
            short result;
            if (!short.TryParse(param, out result)) result = defaultValue;

            return result;
        }

        #endregion

        
        #region Int() int 类型处理

        /// <summary>
        /// 若参数值不介于最小值与最大值之间时返回默认值
        /// </summary>
        /// <param name="p">参数值</param>
        /// <param name="defalutValue">默认值</param>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <returns>返回值</returns>
        public static int Int(string p,int defalutValue,int min,int max)
        {
            var r = Int(p,defalutValue);
            return Int(r, defalutValue, min, max);
        }

        /// <summary>
        /// 若参数值不介于最小值与最大值之间时返回默认值
        /// </summary>
        /// <param name="param">参数值</param>
        /// <param name="defalutValue">默认值</param>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <returns>返回值</returns>
        public static int Int(int param,int defalutValue,int min,int max)
        {
            if (min <= param && param <= max) return param;
            return defalutValue;
        }

        /// <summary>
        /// 将字符串转换为int ，默认值为0
        /// </summary>
        /// <param name="str">需要转换的字符串</param>
        /// <returns>转换结果</returns>
        public static int Int(string str)
        {
            return Int(str, 0);
        }

        /// <summary>
        /// 将字符串转换为 int ， 转换失败时为默认值
        /// </summary>
        /// <param name="p">需要转换的文本</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换结果</returns>
        public static int Int(string p, int defaultValue)
        {
            int result;
            if (!int.TryParse(p, out result)) result = defaultValue;

            return result;
        }

        /// <summary>
        /// 将object转换为Int，默认值(null,转换失败时)为0
        /// </summary>
        /// <param name="obj">需要转换的object</param>
        /// <returns></returns>
        public static int Int(object obj)
        {
            return Int(obj, 0);
        }

        /// <summary>
        /// 将 object 转换为 int ，空对象或转换失败时为默认值
        /// </summary>
        /// <param name="obj">需要转换的object</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换结果</returns>
        public static int Int(object obj, int defaultValue)
        {
            if (obj == null) return defaultValue;

            if (obj.GetType() == typeof(string)) return Int(obj.ToString(), defaultValue);

            if (obj.GetType() == typeof(int)) return (int)obj;

            return defaultValue;
        }

        /// <summary>
        /// 若参数值不介于最小值与最大值之间时返回默认值
        /// </summary>
        /// <param name="obj">参数值</param>
        /// <param name="defalutValue">默认值</param>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <returns>返回值</returns>
        public static int Int(object obj, int defalutValue, int min, int max)
        {
            var r = Int(obj, defalutValue);
            return Int(r, defalutValue, min, max);
        }

        #endregion
        

        #region Long() long 类型处理

        /// <summary>
        /// 范围约束
        /// </summary>
        /// <param name="p">输入</param>
        /// <param name="dv">默认</param>
        /// <param name="min">最小</param>
        /// <param name="max">最大</param>
        /// <returns>返回值</returns>
        public static long Long(object p, long dv, long min, long max)
        {
            var r = Long(p, dv);
            return Long(r, dv, min, max);
        }

        /// <summary>
        /// 范围约束
        /// </summary>
        /// <param name="p">输入</param>
        /// <param name="dv">默认</param>
        /// <param name="min">最小</param>
        /// <param name="max">最大</param>
        /// <returns>返回值</returns>
        public static long Long(string p, long dv, long min, long max)
        {
            var r = Long(p, dv);
            return Long(r, dv, min, max);
        }

        /// <summary>
        /// 范围约束
        /// </summary>
        /// <param name="p">输入</param>
        /// <param name="dv">默认</param>
        /// <param name="min">最小</param>
        /// <param name="max">最大</param>
        /// <returns>返回值</returns>
        public static long Long(long p, long dv, long min, long max)
        {
            if (min <= p && p <= max) return p;
            return dv;
        }

        /// <summary>
        /// 将字符串转换为 long , 默认值为0
        /// </summary>
        /// <param name="param">需要转换的 文本</param>
        /// <returns>转换结果</returns>
        public static long Long(string param)
        {
            return Long(param, 0);
        }

        /// <summary>
        /// 转换Object对，象默认值0
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static long Long(object p)
        {
            return Long(p, 0);
        }

        /// <summary>
        /// 将字符串转换为 long
        /// </summary>
        /// <param name="param">需要转换的 文本</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换结果</returns>
        public static long Long(string param, long defaultValue)
        {
            long result;
            if (!long.TryParse(param, out result)) result = defaultValue;
            return result;
        }

        /// <summary>
        /// 将 object 转换为 long
        /// </summary>
        /// <param name="param">需要转换的 object</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换结果</returns>
        public static long Long(object param, long defaultValue)
        {
            if (param == null) return defaultValue;

            if (param.GetType() == typeof(long)) return Long(param.ToString(), defaultValue);

            if (param.GetType() == typeof(long)) return (int)param;

            return defaultValue;
        }


        #endregion


        #region Decimal() decimal 类型处理

        /// <summary>
        /// 范围约束
        /// </summary>
        /// <param name="p">输入</param>
        /// <param name="dv">默认</param>
        /// <param name="min">最小</param>
        /// <param name="max">最大</param>
        /// <returns>返回值</returns>
        public static decimal Decimal(object p, decimal min, decimal max, decimal dv)
        {
            var r = Decimal(p, dv);
            return Decimal(r, dv, min, max);
        }

        /// <summary>
        /// 范围约束
        /// </summary>
        /// <param name="p">输入</param>
        /// <param name="dv">默认</param>
        /// <param name="min">最小</param>
        /// <param name="max">最大</param>
        /// <returns>返回值</returns>
        public static decimal Decimal(string p, decimal min, decimal max, decimal dv)
        {
            var r = Decimal(p, dv);
            return Decimal(r, dv, min, max);
        }

        /// <summary>
        /// 范围约束
        /// </summary>
        /// <param name="p">输入</param>
        /// <param name="dv">默认</param>
        /// <param name="min">最小</param>
        /// <param name="max">最大</param>
        /// <returns>返回值</returns>
        public static decimal Decimal(decimal p, decimal dv, decimal min, decimal max)
        {
            if (min <= p && p <= max) return p;
            return dv;
        }

        /// <summary>
        /// 将字符串转换为 decimal
        /// </summary>
        /// <param name="param">需要转换的 文本</param>
        /// <returns>转换结果</returns>
        public static decimal Decimal(string param)
        {
            return Decimal(param, 0);
        }

        /// <summary>
        /// 将字符串转换为 decimal
        /// </summary>
        /// <param name="param">需要转换的 文本</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换结果</returns>
        public static decimal Decimal(string param, decimal defaultValue)
        {
            decimal result;
            if (!decimal.TryParse(param, out result)) result = defaultValue;
            return result;
        }

        /// <summary>
        /// 将 object 转换为 decimal , 默认值为0
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static decimal Decimal(object p)
        {
            return Decimal(p,0);
        }

        /// <summary>
        /// 将 object 转换为 decimal
        /// </summary>
        /// <param name="param">需要转换的 object</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换结果</returns>
        public static decimal Decimal(object param, decimal defaultValue)
        {
            if (param == null) return defaultValue;

            if (param.GetType() == typeof(decimal)) return Decimal(param.ToString(), defaultValue);

            if (param.GetType() == typeof(decimal)) return (decimal)param;

            return defaultValue;
        }


        #endregion


        #region DateTime()   DateTime类型处理

        /// <summary>
        /// 将对象转为为时间类型 , 默认为System.DateTime.MinValue
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static DateTime? DateTime(object p)
        {
            return DateTime(p, System.DateTime.MinValue);
        }

        /// <summary>
        /// 将对象转为为时间类型
        /// </summary>
        /// <param name="p">输入</param>
        /// <param name="defaultValue">默认</param>
        /// <param name="min">最小</param>
        /// <param name="max">最大</param>
        /// <returns></returns>
        public static DateTime? DateTime(object p,DateTime? defaultValue,DateTime min,DateTime max)
        {
            var r = DateTime(p, defaultValue);
            return DateTime(r, defaultValue, min, max);
        }

        /// <summary>
        /// 将对象转为为时间类型
        /// </summary>
        /// <param name="p">输入</param>
        /// <param name="defaultValue">默认</param>
        /// <returns></returns>
        public static DateTime? DateTime(object  p,DateTime? defaultValue)
        {
            if (p == null) return defaultValue;

            if (p.GetType() == typeof(string)) return DateTime(p.ToString(), defaultValue);

            if (p.GetType() == typeof(DateTime)) return (DateTime)p;

            return defaultValue;
        }

        /// <summary>
        /// 将字符串转为时间类型
        /// </summary>
        /// <param name="p">输入</param>
        /// <param name="defaultValue">默认</param>
        /// <param name="min">最小</param>
        /// <param name="max">最大</param>
        /// <returns></returns>
        public static DateTime DateTime(string p,DateTime defaultValue,DateTime min,DateTime max)
        {
            var r = DateTime(p, defaultValue);
            return DateTime(r, defaultValue, min, max);
        }

        /// <summary>
        /// 将字符串转为时间类型
        /// </summary>
        /// <param name="p">输入</param>
        /// <param name="defaultValue">默认</param>
        /// <param name="min">最小</param>
        /// <param name="max">最大</param>
        /// <returns></returns>
        public static DateTime DateTime(DateTime p,DateTime defaultValue,DateTime min,DateTime max)
        {
            if (min <= p && p <= max) return p;
            return defaultValue;
        }

        /// <summary>
        /// 将字符串转换为 DateTime. 默认值为的DateTime.MinValue时间
        /// </summary>
        /// <param name="param">需要转换的文本</param>
        /// <returns>转换结果</returns>
        public static DateTime DateTime(string param)
        {
            return DateTime(param, System.DateTime.MinValue);
        }

        /// <summary>
        /// 将字符串转换为 DateTime.
        /// </summary>
        /// <param name="param">需要转换的文本</param>
        /// <param name="defaultValue">默认时间</param>
        /// <returns>转换结果</returns>
        public static DateTime DateTime(string param, DateTime defaultValue)
        {
            DateTime result;
            if (!System.DateTime.TryParse(param, out result))
                result = defaultValue;
            return result;
        }

        /// <summary>
        /// 将字符串转换为 DateTime.
        /// </summary>
        /// <param name="str">需要转换的文本</param>
        /// <param name="defaultValue">默认时间</param>
        /// <returns>结果</returns>
        public static DateTime? DateTime(string str,DateTime? defaultValue)
        {
            try
            {
                DateTime result;
                if (!System.DateTime.TryParse(str, out result))
                    return null;
                return result;
            }
            catch
            {
                return defaultValue;
            }
        }


        /// <summary>
        /// 带时区的时间转换
        /// </summary>
        /// <param name="param">要转换的字符串</param>
        /// <param name="defaultValue">默认值，可为null</param>
        /// <returns>可能为null的时区时间</returns>
        public static DateTimeOffset? DateTime(string param, DateTimeOffset? defaultValue)
        {
            try
            {
                DateTimeOffset result;
                if (!DateTimeOffset.TryParse(param, out result))
                    return null;
                return result;
            }
            catch
            {
                return defaultValue;
            }
        }

        #endregion


        #region string  String(string)   \  SafeString(string)   \  SafeLikeString(string)

        /// <summary>
        /// 移除开始和末尾的空白字符
        /// </summary>
        /// <param name="p">需要处理的文本</param>
        /// <returns>处理结果</returns>
        public static string String(string p)
        {
            return String(p, string.Empty);
        }

        /// <summary>
        /// 空串或null时返回默认值
        /// </summary>
        /// <param name="p">需要处理的文本</param>
        /// <param name="defaultValue">默认串</param>
        /// <returns>处理结果</returns>
        public static string String(string p, string defaultValue)
        {
            return !System.String.IsNullOrEmpty(p) ? p.Trim() : defaultValue;
        }


        /// <summary>
        /// Sql 安全过滤
        /// </summary>
        /// <param name="param">需要处理的文本</param>
        /// <returns>处理结果</returns>
        public static string SafeString(string param)
        {
            var result = String(param);
            if (!System.String.IsNullOrEmpty(result))
            {
                result = result.Replace("\'", "");
            }
            return result;
        }


        /// <summary>
        /// Sql 安全过滤(颜色)
        /// </summary>
        /// <param name="text">需要处理的文本</param>
        /// <returns>处理结果</returns>
        public static string SafeLikeString(string text)
        {
            char[] filter = { '\'', '%', '_', '[', ']' };
            foreach (var item in filter)
            {
                while (true)
                {
                    var pos = text.IndexOf(item);
                    if (pos > -1)
                        text = text.Remove(pos, 1);
                    else
                        break;
                }
            }
            return text;
        }


        #endregion


        #region int[] IntArray(string)  \  IntArray(string,char)   \  IntArray(string[])  \ Distinct<T>(T[])

        /// <summary>
        /// 将字符串转换为 int[]
        /// </summary>
        /// <param name="param">需要转换的文本</param>
        /// <returns>转换结果</returns>
        public static int[] IntArray(string param)
        {
            return IntArray(param, ",");
        }

        ///<summary>
        /// 将字符串转换为 int[]
        ///</summary>
        ///<param name="str">参数</param>
        ///<param name="spliter">分隔符</param>
        ///<returns></returns>
        public static int[] IntArray(string str, string spliter)
        {
            return System.String.IsNullOrEmpty(str) ? new int[0] : IntArray(str.Split(spliter.ToCharArray()));
        }

        /// <summary>
        /// 将 string[] 转换为 int[]
        /// </summary>
        /// <param name="param">需要转换的 string[]</param>
        /// <returns>转换结果</returns>
        public static int[] IntArray(string[] param)
        {
            return Array.ConvertAll(param, new Converter<string, int>(
                                               Int
                                               ));
        }

        /// <summary>
        /// 剔除数组中重复项
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static T[] Distinct<T>(T[] source) where T : struct
        {
            if (source == null || source.Length < 2) return source;
            var result = new List<T>();
            foreach (var i in source)
            {
                var i1 = i;
                if (!result.Exists(item => i1.Equals(item)))
                    result.Add(i);
            }
            return result.ToArray();
        }

        #endregion


        #region string <--> int[]  Join(string,int[])  Join(string,List<int>)  Join(int[])  StringToIdIntArray  StringToPositiveIntArray

        /// <summary>
        /// 将 int[] 转换 文本
        /// </summary>
        /// <param name="separator">转换后的配接分隔符(可为null)</param>
        /// <param name="array">需要转换的 int[]</param>
        /// <returns>转换后得到的文本</returns>
        public static string Join(string separator, int[] array)
        {
            var result = Array.ConvertAll(array, input => input.ToString());       //int[] -> string[]
            return System.String.Join(separator, result);
        }

        ///<summary>
        /// 将 List&lt;int&gt;  转换 文本
        ///</summary>
        ///<param name="separator">分隔符</param>
        ///<param name="list">List&lt;int&gt;</param>
        ///<returns></returns>
        public static string Join(string separator, List<int> list)
        {
            return Join(separator, list.ToArray());
        }
        ///<summary>
        /// 将 int[] 转换 文本
        ///</summary>
        ///<param name="array">int数组</param>
        ///<returns></returns>
        public static string Join(int[] array)
        {
            return Join(null, array);
        }


        /// <summary>
        /// 将 字符串 转换为 大于0  int[]
        /// </summary>
        /// <param name="separator">文本的分隔符</param>
        /// <param name="str">需要转换的文本</param>
        /// <returns>转换后得到的 int[]</returns>
        public static int[] StringToIdIntArray(string separator, string str)
        {
            var ids = str.Split(separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            var array = new List<int>();

            foreach (var item in ids)
            {
                var result = Int(item, 0);
                if (result > 0)
                    array.Add(result);
            }
            return array.ToArray();
        }
        /// <summary>
        /// 将 字符串 转换为 大于0  int[]
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int[] StringToIdIntArray(string str)
        {
            return StringToIdIntArray(",", str);
        }

        /// <summary>
        /// 将 字符串 转换为 大于 -1 的 int[]
        /// </summary>
        /// <param name="separator">文本的分隔符</param>
        /// <param name="str">需要转换的文本</param>
        /// <returns>转换后得到的 int[]</returns>
        public static int[] StringToPositiveIntArray(string separator, string str)
        {
            var ids = str.Split(separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            var array = new List<int>();
            foreach (var item in ids)
            {
                var result = Int(item, 0);
                if (result > -1)
                    array.Add(result);
            }
            return array.ToArray();
        }
        ///<summary>
        /// 把字符串转为大于 -1 的 int[]
        /// 字符串用 , 隔开
        /// 负数将被剔除
        ///</summary>
        ///<param name="str">字符串列<example>1,9,20,30</example></param>
        ///<returns></returns>
        public static int[] StringToPositiveIntArray(string str)
        {
            return StringToPositiveIntArray(",", str);
        }


        #endregion
        

        #region Enum  T Enum<T>(string)   \    T Enum<T>(string,T)    \  T Enum<T>(int)   T Enum<T>(int,T)

        /// <summary>
        /// 将字符串转换为 枚举
        /// </summary>
        /// <typeparam name="T">目标枚举类型</typeparam>
        /// <param name="param">需要转换的文本</param>
        /// <returns>转换结果</returns>
        public static T Enum<T>(string param)
        {
            T result;
            try
            {
                result = (T)System.Enum.Parse(typeof(T), param, true);
            }
            catch
            {
                result = default(T);
            }
            return result;
            //return Enum<T>(param, default(T));
        }

        /// <summary>
        /// 将字符串转换为 枚举
        /// </summary>
        /// <typeparam name="T">目标枚举类型</typeparam>
        /// <param name="param">需要转换的文本</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换结果</returns>
        public static T Enum<T>(string param, T defaultValue)
        {
            T result;
            try
            {
                result = (T)System.Enum.Parse(typeof(T), param, true);
            }
            catch
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// 将 数值 转换为 枚举
        /// </summary>
        /// <typeparam name="T">目标枚举类型</typeparam>
        /// <param name="param">需要转换的文本</param>
        /// <returns>转换结果</returns>
        public static T Enum<T>(int param)
        {
            return Enum<T>(param + "");
        }

        /// <summary>
        /// 将 数值 转换为 枚举
        /// </summary>
        /// <typeparam name="T">目标枚举类型</typeparam>
        /// <param name="param">需要转换的文本</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换结果</returns>
        public static T Enum<T>(int param, T defaultValue)
        {
            return Enum(param + "", defaultValue);
        }

        #endregion


        #region T  Convert<T>(string)

        /// <summary>
        /// 将字符串转换为 目标类型 (有装箱拆箱的损失)
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="p">需要转换的文本</param>
        /// <returns>转换结果</returns>
        public static T Convert<T>(string p)
        {
            T result;
            switch (Type.GetTypeCode(typeof(T)))
            {
                case TypeCode.Byte:
                    result = (T)(object)Byte(p);
                    break;
                case TypeCode.Int32:
                    result = (T)(object)Int(p);
                    break;
                default:
                    result = default(T);
                    break;
            }
            return result;
        }

        #endregion




        #region byte[] <---> string 待测试??

        /// <summary>
        /// 16进制文本转换为 byte[].
        /// </summary>
        /// <param name="hexString">需要转换的 16进制文本.</param>
        /// <returns>转换后得到的 byte[]</returns>
        public static byte[] HexStringToBitArray(String hexString)
        {
            var numberChars = hexString.Length;
            var bytes = new byte[numberChars / 2];
            for (var i = 0; i < numberChars; i += 2)
            {
                bytes[i / 2] = System.Convert.ToByte(hexString.Substring(i, 2), 16);
            } return bytes;
        }



        #endregion



        #region others

        ///// <summary>
        ///// 把 IDataRecord 转换为 目标类型
        ///// </summary>
        ///// <typeparam name="T">目标类型</typeparam>
        ///// <param name="dr">IDataRecord 实例</param>
        ///// <returns>转换结果</returns>
        //public static T Create<T>(IDataRecord dr) where T : IDataComponent, new()
        //{
        //    T result = new T();
        //    result.Parse(dr);
        //    return result;
        //}


        ///// <summary>
        ///// 将 byte[] 转换为 16进制文本.
        ///// </summary>
        ///// <param name="buf">需要转换的 byte[]</param>
        ///// <returns>转换后得到的 16进制文本.</returns>
        //public static string ByteArrayToHexString(byte[] buf)
        //{
        //    return ByteArrayToHexString(buf, buf.Length);
        //}

        ///// <summary>
        ///// 将 byte[] 转换为 16进制文本.
        ///// </summary>
        ///// <param name="buf">需要转换的 byte[]</param>
        ///// <param name="iLen">转换长度</param>
        ///// <returns>转换后得到的 16进制文本.</returns>
        ////public static string ByteArrayToHexString(byte[] buf, int iLen)
        ////{

        ////    return (string)ByteArrayToHexStringMethod().Invoke(null, new object[] { buf, iLen });
        ////}

        ////static MethodInfo byteArrayToHexString;

        ///// <summary>
        ///// 反射调用该方法, 并且缓存.
        ///// </summary>
        ///// <returns></returns>
        //static MethodInfo ByteArrayToHexStringMethod()
        //{
        //    if (byteArrayToHexString == null)
        //    {
        //        Type type = typeof(System.Web.Configuration.MachineKeySection);
        //        byteArrayToHexString = type.GetMethod("ByteArrayToHexString", BindingFlags.Static | BindingFlags.NonPublic);
        //    }
        //    return byteArrayToHexString;
        //}



        //public static K[] ParseIDArray<T, K>(ComponentCollection<T> obj, ParseAction<T, K> action) where T : IComponent
        //{
        //    if (obj.Count == 0) return null;
        //    if (obj.Count == 1) return new K[] { action(obj[0]) };
        //    List<K> result = new List<K>();
        //    foreach (T item in obj)
        //        result.Add(action(item));
        //    return result.ToArray();
        //}

        #endregion

    }
}